﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using cc2530.Lib;
using cc2530.Lib.device;
using cc2530.Lib.contiki;


namespace cc2530
{
    public partial class SerialSDisplay : Form
    {
        public SerialSDisplay()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Clear();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            //base.OnClosing(e);
            e.Cancel = true;
            this.Hide();
        }

        public void Display(object sender, TransationBase args)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                Ulitily.OnTransferHandler handler = new Ulitily.OnTransferHandler(Display);
                this.richTextBox1.BeginInvoke(handler, new object[] { sender, args });
            }
            else
            {
                if (this.checkBox2.Checked == false) return;
                IPv6Packet packet = new IPv6Packet(args.data);
                if (this.checkBox1.Checked == true)
                {
                    try
                    {
                        if (packet.IPv6header.Version == 6)
                        {
                            packet.hardType = HardType.IPv6;
                            this.richTextBox1.AppendText(packet.HeaderInfor() + packet.icmpInfor);
                            return;
                        }
                    }
                    catch { }
                }

                this.richTextBox1.AppendText(
                    System.DateTime.Now.ToString("[hh:mm:ss]") + "(" + args.datalen + ")"
                    + Ulitily.ShareClass.hexByteArrayToString(args.data).Replace("-", " ")
                    + "\r\n");
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
